<?php

require_once( "common.inc" );
define( "NM_TYPE_CODE_REPORT", 11 );

class NMMarker
{
	private $_data;
	private $_reports;
	private $_rosterId;
	private $_latestTime;
	
	public static $UniqueId = 0;
	
	function __construct( $data )
	{
		$this->_data = $data;
		$this->_rosterId = $data[ "roster_id" ];
		if ( isset( $data[ "type_code" ] ) )
		{
			$this->_rosterId .= self::$UniqueId;
			self::$UniqueId++;
		}
	}
	
	function IsSameLocation( $data )
	{
		return $this->_data[ "latitude" ] == $data[ "latitude" ] && $this->_data[ "longitude" ] == $data[ "longitude" ];
	}
	
	function HasLocation()
	{
		return $this->_data[ "latitude" ] != 0 && $this->_data[ "longitude" ] != 0;
	}
	
	function AddReport( $data )
	{
		$this->_reports[] = $data;
		if ( ! isset( $this->_latestTime ) )
			$this->_latestTime = $data[ "activity_time" ];
	}
	
	function GetIconName()
	{
		if ( isset( $this->_data[ "type_code" ] ) )
			return "weather_lightning.png";
		else if ( $this->_data[ "is_mobile" ] )
		{
			if ( isset( $this->_reports ) )
				return "car_rpt.png";
			else
				return "car.png";
		}
		else if ( isset( $this->_reports ) )
			return "transmit_blue_rpt.png";
		else
			return "transmit_blue.png";
	}
	
	function GetLatestTime()
	{
		if ( isset( $this->_latestTime ) )
			return $this->_latestTime;
		else
			return $this->_data[ "time_in" ];
	}
	
	function GetRosterId()
	{
		return $this->_rosterId;
	}
	
	function GetName()
	{
		return $this->_data[ "name" ];
	}
	
	function IsNetControl()
	{
		if ( isset( $this->_data[ "is_net_control" ] ) )
			return $this->_data[ "is_net_control" ];
		else
			return FALSE;
	}
	
	function IsAltNetControl()
	{
		if ( isset( $this->_data[ "is_alt_control" ] ) )
			return $this->_data[ "is_alt_control" ];
		else
			return FALSE;
	}
	
	function IsSKYWARN()
	{
		if ( isset( $this->_data[ "is_skywarn" ] ) )
			return $this->_data[ "is_skywarn" ];
		else
			return FALSE;
	}
	
	function IsActive()
	{
		if ( isset( $this->_data[ "is_active" ] ) )
			return $this->_data[ "is_active" ];
		else
			return FALSE;
	}
	
	function GetInfoWindowContent()
	{
		global $NMZoneUtc, $NMZoneLocal;
		
		if ( isset( $this->_reports ) )
		{
			$infoContent = "<table>";
			foreach ( $this->_reports as $report )
			{
				$rosterId = $report[ "roster_id" ];
				$activityTime = new DateTime( $report[ "activity_time" ], $NMZoneUtc );
				$activityTime->setTimezone( $NMZoneLocal );
				$time = $activityTime->format( "H:i" );
				$details = htmlspecialchars( stripslashes( $report[ "details" ] ) );
				$infoContent .= "<tr><td>$rosterId</td><td>$time</td><td>$details</td></tr>";
			}
			$infoContent .= "</table>";
			return $infoContent;
		}
		else
			return "<p>No reports for {$this->_data[ 'roster_id' ]}</p>";
	}
	
	function WriteScript()
	{
		if ( $this->HasLocation() )
		{
			$infoContent = $this->GetInfoWindowContent();
			$title = $this->GetRosterId();
			$icon = $this->GetIconName();
			echo <<<EOS
position = new google.maps.LatLng( {$this->_data[ "latitude" ]}, {$this->_data[ "longitude" ]} );
marker = new google.maps.Marker( { position: position, map: map, title: "$title", icon: "$icon" } );
marker.info = "$infoContent";
onMarkerClick = "selectMarker( '$title' )";
google.maps.event.addListener( marker, "click", new Function( onMarkerClick ) );
markers[ "$title" ] = marker;

EOS;
		}
	}
	
	function Write()
	{
		global $NMZoneUtc, $NMZoneLocal;
		
		if ( ! isset( $this->_data[ "type_code" ] ) )
		{
			$rosterId = $this->_data[ "roster_id" ];
			$name = $this->_data[ "name" ];
			$location = htmlspecialchars( stripslashes( $this->_data[ "location" ] ) );
			$timeIn = new DateTime( $this->_data[ "time_in" ], $NMZoneUtc );
			$timeIn->setTimezone( $NMZoneLocal );
			$time = $timeIn->format( "r" );
			$content = "$rosterId ($name), $location<br/>Checked in $time";
			$html = new NMHtmlItem( "p", $content );
			if ( $this->HasLocation() )
			{
				$html->AddAttribute( "class", "marker" );
				$html->AddAttribute( "onclick", "selectMarker( '$rosterId' )" );
			}
			$html->Write();
		}
	}
	
	function WriteCallsignAndName( $prefix )
	{
		$rosterId = $this->_data[ "roster_id" ];
		$name = $this->_data[ "name" ];
		$content = "$prefix: $rosterId ($name)";
		$html = new NMHtmlItem( "p", $content );
		$html->Write();
	}
}

class NMActivity
{
	private $_data;
	private $_name;
	
	function __construct( $data )
	{
		$this->_data = $data;
	}
	
	function GetTime()
	{
		return $this->_data[ "activity_time" ];
	}
	
	function SetName( $name )
	{
		$this->_name = $name;
	}
	
	function Write()
	{
		global $NMZoneUtc, $NMZoneLocal;
		
		$activityTime = new DateTime( $this->_data[ "activity_time" ], $NMZoneUtc );
		$activityTime->setTimezone( $NMZoneLocal );
		$time = $activityTime->format( "H:i" );
		$rosterId = $this->_data[ "roster_id" ];
		if ( isset( $this->_name ) )
			$rosterId .= " ($this->_name)";
		$location = htmlspecialchars( stripslashes( $this->_data[ "location" ] ) );
		$typeDescrip = $this->_data[ "type_descrip" ];
		$details = htmlspecialchars( stripslashes( $this->_data[ "details" ] ) );
		$isPriority = $this->_data[ "is_priority" ];
		$content = "<td>$time</td><td>$rosterId</td><td>$location</td><td>$typeDescrip</td><td>$details</td>";
		$html = new NMHtmlItem( "tr", $content );
		if ( $isPriority )
			$html->AddAttribute( "class", "priority" );
		$html->Write();
	}
}

function NMWriteScript()
{
	global $markers;
	
	if ( isset( $markers ) )
	{
		echo <<<EOS
function initialize()
{
var position;
var marker;
var onMarkerClick;
initializeMap();

EOS;
		foreach ( $markers as $marker )
		{
			$marker->WriteScript();
		}
		echo "\n}";
	}
}

function NMCompareMarkers( $marker1, $marker2 )
{
	$time1 = $marker1->GetLatestTime();
	$time2 = $marker2->GetLatestTime();
	if ( $time1 > $time2 )
		return -1;
	else if ( $time2 > $time1 )
		return 1;
	else
		return 0;
}

function NMCompareActivities( $act1, $act2 )
{
	$time1 = $act1->GetTime();
	$time2 = $act2->GetTime();
	if ( $time1 > $time2 )
		return -1;
	else if ( $time2 > $time1 )
		return 1;
	else
		return 0;
}

function NMWriteRoster()
{
	global $markers, $netControl, $altNetControl, $skywarn, $rosterTotal;
	$writeRule = FALSE;
	
	if ( isset( $markers ) )
	{
		if ( isset( $netControl ) )
		{
			$netControl->WriteCallsignAndName( "Net Control" );
			$writeRule = TRUE;
		}
		if ( isset( $altNetControl ) )
		{
			$altNetControl->WriteCallsignAndName( "Alternate Net Control" );
			$writeRule = TRUE;
		}
		if ( isset( $skywarn ) )
		{
			$skywarn->WriteCallsignAndName( "SKYWARN Liaison" );
			$writeRule = TRUE;
		}
		if ( $writeRule )
			echo "<hr/>\n";
		$html = new NMHtmlItem( "p", "Roster total: $rosterTotal" );
		$html->Write();
		usort( $markers, "NMCompareMarkers" );
		foreach ( $markers as $marker )
		{
			$marker->Write();
		}
	}
}

function NMWriteActivities()
{
	global $activities;
	
	if ( isset( $activities ) )
	{
		usort( $activities, "NMCompareActivities" );
		foreach ( $activities as $activity )
		{
			$activity->Write();
		}
	}
}

if ( NMOpenDB() && isset( $_GET[ "net_id" ] ) )
{
	$netId = ( int ) $_GET[ "net_id" ];
	$rosterTotal = 0;
	if ( $data = mysql_query( "SELECT * FROM roster WHERE net_id = $netId" ) )
	{
		while ( $row = mysql_fetch_assoc( $data ) )
		{
			$rosterTotal++;
			$marker = new NMMarker( $row );
			$markers[ $marker->GetRosterId() ] = $marker;
			if ( $marker->IsNetControl() )
				$netControl = $marker;
			if ( $marker->IsAltNetControl() )
				$altNetControl = $marker;
			if ( $marker->IsSKYWARN() )
				$skywarn = $marker;
		}
	}
	if ( $data = mysql_query( "SELECT * FROM activity WHERE net_id = $netId ORDER BY latitude, longitude, activity_time DESC" ) )
	{
		while ( $row = mysql_fetch_assoc( $data ) )
		{
			$newActivity = new NMActivity( $row );
			$rosterId = $row[ "roster_id" ];
			if ( isset( $markers[ $rosterId ] ) )
				$newActivity->SetName( $markers[ $rosterId ]->GetName() );
			if ( $row[ "type_code" ] == NM_TYPE_CODE_REPORT )
			{
				if ( isset( $markers[ $rosterId ] ) && $markers[ $rosterId ]->IsSameLocation( $row ) )
				{
					$lastMarker = $markers[ $rosterId ];
				}
				else if ( ! isset( $lastMarker ) || ! $lastMarker->IsSameLocation( $row ) )
				{
					$lastMarker = new NMMarker( $row );
					$markers[ $lastMarker->GetRosterId() ] = $lastMarker;
				}
				$lastMarker->AddReport( $row );
			}
			$activities[] = $newActivity;
		}
	}
}

?>
